Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use max_by for longest_cmd_name_length #628

Merged

Conversation

andyw8
Copy link
Contributor

@andyw8 andyw8 commented Jul 4, 2023

A small refactoring for readability.

https://rubyapi.org/3.2/o/enumerable#method-i-max_by

@@ -14,7 +14,7 @@ class ShowCmds < Nop
def execute(*args)
commands_info = IRB::ExtendCommandBundle.all_commands_info
commands_grouped_by_categories = commands_info.group_by { |cmd| cmd[:category] }
longest_cmd_name_length = commands_info.map { |c| c[:display_name] }.max { |a, b| a.length <=> b.length }.length
longest_cmd_name_length = commands_info.map { |c| c[:display_name] }.max_by(&:length).length
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about
map { |c| c[:display_name] }.map(&:length).max
or
map { |c| c[:display_name].length }.max

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, I've pushed an update.

@andyw8 andyw8 force-pushed the andyw8/use-max-by-for-longest_cmd_name_length branch from 96944b6 to f17b038 Compare July 4, 2023 20:09
Copy link
Member

@st0012 st0012 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks 👍

@st0012 st0012 merged commit 5e87f3b into ruby:master Jul 4, 2023
matzbot pushed a commit to ruby/ruby that referenced this pull request Jul 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants